<?php

class system_queue_consumer {

    private static $__instance = NULL;

    private $__consumer = NULL;

    function __construct($mode) {
        // $consumer = kernel::single("system_queue_consumer_".$mode);
        $class = 'system_queue_consumer_' . $mode;
        $consumer = new $class();
        if ($consumer instanceof system_interface_queue_IConsumer) {
            $this->__consumer = &$consumer;
        } else {
            throw new Exception("The consumer must implements system_interface_queue_IConsumer!");
        }
    }

    public static function instance($mode = NULL) {
        if (empty($mode)) {
            if (defined("QUEUE_CONSUMER") && constant("QUEUE_CONSUMER")) {
                $mode = QUEUE_CONSUMER;
            } else {
                $mode = 'fork';
            }
        }
        
        if (! isset(self::$__instance[$mode])) {
            if ($instance = new system_queue_consumer($mode)) {
                self::$__instance[$mode] = $instance;
                return self::$__instance[$mode];
            } else {
                return false;
            }
        } else {
            return self::$__instance[$mode];
        }
    }

    public function exec($queue_name, $max = 5, $phpExec = '') {
        $this->__consumer->exec($queue_name, $max, $phpExec);
    }
}